/**
 * @file   LoanAuthLoadDB.java
 * @date   2012-12-18上午11:45:14
 * @author user
 */
package com.gzceb.zhgjj.fileload.impl;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gzceb.zhgjj.filecheck.FileCheckConstants;
import com.gzceb.zhgjj.fileload.AbstractFileLoad;
import com.gzceb.zhgjj.service.LoanAprvService;
import com.gzceb.zhgjj.service.LoanAuthService;
import com.gzceb.zhgjj.util.SpringContextUtil;
import com.gzceb.zhgjj.util.Utils;
import com.gzceb.zhgjj.web.entity.LoanAprv;
import com.gzceb.zhgjj.web.entity.LoanAuth;

/**
 * <p>贷款授权文件加载进数据库类。</p>
 *
 * @date   2012-12-18上午11:45:14 
 * @author liqing
 * @description 贷款授权文件的响应文件读取后，加载进数据库表  info_loanauth， 并刷新对应
 *              info_loanaprv 贷款审批表中 贷款授权状态 字段
 */
public class LoanAuthLoadDB extends AbstractFileLoad {

	private static Log logger = LogFactory.getLog(LoanAuthLoadDB.class);
	private LoanAuth loanAuth;
	private LoanAprv loanAprv;  // 记录授权时，也刷新审批文件列表中对应记录的贷款授权状态
	private String absFileName; // 响应文件的绝对路径
	private List<String> fileList;  // 筛选的审批记录中需要存库的记录
	
	public LoanAuthLoadDB(String absFileName){
		this.absFileName = absFileName;
		fileList = new ArrayList<String>();
	}
	
	
	public LoanAuth getLoanAuth() {
		return loanAuth;
	}


	public void setLoanAuth(LoanAuth loanAuth) {
		this.loanAuth = loanAuth;
	}


	public LoanAprv getLoanAprv() {
		return loanAprv;
	}


	public void setLoanAprv(LoanAprv loanAprv) {
		this.loanAprv = loanAprv;
	}


	/* (non-Javadoc)
	 * @see com.gzceb.zhgjj.fileload.AbstractFileLoad#run()
	 */
	@Override
	public void run() {
		readFile(absFileName);
		saveToDB();

	}

	/* (non-Javadoc)
	 * @see com.gzceb.zhgjj.fileload.AbstractFileLoad#saveToDB()
	 */
	@Override
	public void saveToDB() {
		LoanAuthService laus = (LoanAuthService)SpringContextUtil.getBean("loanAuthService");
		LoanAprvService laps = (LoanAprvService) SpringContextUtil.getBean("loanAprvService");
		
		boolean rst = false;
		
		if ( 0 == fileList.size()){
			logger.info("the rsp file has no record to DB: " + absFileName);
		}else{
			// 从List中开始读取每一行的授权撤销记录，循环记入库表，并刷新审批表中贷款授权状态字段
			for(int i = 0 ; i< fileList.size(); i++){
				loanAuth.setContcode(fileList.get(i).substring(0, 20));
				loanAuth.setCustname(fileList.get(i).substring(20, 30).trim());
				loanAuth.setPaperkind(fileList.get(i).substring(30, 32));
				loanAuth.setPaperid(fileList.get(i).substring(32, 52));
				loanAuth.setAmt(Double.parseDouble(fileList.get(i).substring(52, 64)));
				loanAuth.setTerm(Integer.parseInt(fileList.get(i).substring(64, 72)));
				loanAuth.setRate(Double.parseDouble(fileList.get(i).substring(72, 83)));
				loanAuth.setRetutype(fileList.get(i).substring(83, 87));
				loanAuth.setIndiissuedate(fileList.get(i).substring(87, 95));
				loanAuth.setInptime(Utils.getCurrDateStr19Str());
				loanAuth.setIsrevoked("0");  // 记录授权时，先默认授权状态为 0-未撤销
				// 更新贷款审批表中的记录
				loanAprv.setContcode(fileList.get(i).substring(0, 20));
				loanAprv.setAuthorstat("1"); // 记录授权时，先默认授权状态为 1-已授权
				
				rst = laus.saveToDB(loanAuth) && laps.updateToDB(loanAprv);
				if(false == rst){
					logger.error("failed to save and update record to DB . Record : " + fileList.get(i));
				}
				
			}
		}
		

	}


	@Override
	public void readFile(String absFileName) {
		// 将响应文件的内容去除不需存库的记录，生成List
		BufferedReader br = null;
		logger.info("begin to read rsp file: " + absFileName);
		try{
			br = new BufferedReader(new FileReader(absFileName));
			String tmpStr = null;
			boolean first = false;
			while( (tmpStr = br.readLine()) != null){
				if( false == first ){
					// 第一行信息是总记录，跳过
					first = true;
					continue;
				}
				// 响应文件中每一行的末尾最后一个字节如果为9，表示接受，需要存库，记录到 List中
				if( tmpStr.endsWith(FileCheckConstants.RT_ACCEPT.getValue()) ){
					fileList.add(tmpStr);
				}
			}
		} catch (Exception e){
			logger.error("error happened when read rsp file: " + absFileName);
		} finally{
			if (br != null) {
				try {
					br.close();
				} catch (Exception ec) {
					logger.error("Errors happened when closinig BufferedReader.\n" + ec);
				}
			}
		}
		
		logger.info("the rsp file has been read to List. The list size is : " + fileList.size());
		
	}

}
